package com.sbio.udiapp.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.sbio.udiapp.R;

import java.util.List;

public class PackingListViewAdapter extends BaseAdapter {

    private Context context;

    private View box_code_scan;

    private List<String> datas;

    public PackingListViewAdapter(Context context, View box_code_scan, List<String> datas) {
        this.context = context;
        this.box_code_scan = box_code_scan;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();

            convertView = LayoutInflater.from(context).inflate(R.layout.activity_packing_listview, parent, false);
            viewHolder.tv = convertView.findViewById(R.id.tv_box_code);
            viewHolder.remove = convertView.findViewById(R.id.remove);
            viewHolder.remove.setOnClickListener(viewHolder.removeListener);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv.setText(datas.get(position));
        /*
        if (position % 2 == 0) {
            viewHolder.tv.setBackgroundColor(0XFFF5F5F5);
        } else {
            viewHolder.tv.setBackgroundColor(0XFFFFFFFF);
        }*/

        //获取焦点
        /*((Activity)context).runOnUiThread(()->{
            try {
                Thread.sleep(1000);
                box_code_scan.requestFocus();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });*/
//        box_code_scan.requestFocus();
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    class ViewHolder{
        TextView tv;
        Button remove;

        RemoveListener removeListener;

        public ViewHolder() {
            removeListener = new RemoveListener();
        }

        class RemoveListener implements View.OnClickListener{

            @Override
            public void onClick(View v) {
                datas.remove(tv.getText().toString());
                notifyDataSetChanged();
            }
        }
    }
}
